package com.easemob.chat;

import android.content.ContentValues;
import android.text.TextUtils;
import com.easemob.EMCallBack;
import com.easemob.chat.core.a;
import com.easemob.chat.core.e;
import com.easemob.chat.core.f;
import com.easemob.chat.core.f.c;
import com.easemob.chat.core.m;
import com.easemob.cloud.CloudOperationCallback;
import com.easemob.cloud.HttpFileManager;
import com.easemob.exceptions.EaseMobException;
import com.easemob.util.EMLog;
import com.easemob.util.PathUtil;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Message.Type;

class EMMessageHandler
  implements m
{
  private static final String TAG = EMMessageHandler.class.getSimpleName();
  private static final EMMessageHandler me = new EMMessageHandler();
  ExecutorService sendThreadPool = Executors.newCachedThreadPool();

  public static EMMessageHandler getInstance()
  {
    return me;
  }

  public void sendGroupMessage(EMMessage paramEMMessage, EMCallBack paramEMCallBack)
  {
    try
    {
      paramEMMessage.setChatType(EMMessage.ChatType.GroupChat);
      if (paramEMMessage.msgId == null)
        paramEMMessage.msgId = EMMessageUtils.getUniqueMessageId();
      if (paramEMMessage.getType() != EMMessage.Type.CMD)
        EMConversationManager.getInstance().saveMessage(paramEMMessage);
      paramEMMessage.status = EMMessage.Status.INPROGRESS;
      paramEMMessage.from = EMSessionManager.getInstance().currentUser;
      String str = paramEMMessage.getTo();
      EMLog.d(TAG, "start send group message:" + str + " message:" + paramEMMessage.toString());
      localObject = new EMSendMessageRunnable(str, paramEMMessage, paramEMCallBack);
      this.sendThreadPool.execute((Runnable)localObject);
    }
    catch (Exception localException)
    {
      paramEMMessage.status = EMMessage.Status.FAIL;
      Object localObject = new ContentValues();
      ((ContentValues)localObject).put("status", paramEMMessage.status.ordinal());
      e.a().a(paramEMMessage.msgId, (ContentValues)localObject);
      localException.printStackTrace();
      if (paramEMCallBack != null)
        EMMessageUtils.asyncCallback(paramEMCallBack, -2, localException.getLocalizedMessage());
    }
  }

  public void sendMessage(Chat paramChat, EMMessage paramEMMessage)
    throws EaseMobException
  {
    sendMessage(paramChat, paramEMMessage, null);
  }

  public void sendMessage(Chat paramChat, EMMessage paramEMMessage, EMCallBack paramEMCallBack)
  {
    int i = EMMessageUtils.checkMessageError(paramEMMessage);
    Object localObject;
    if (i != 0)
    {
      paramEMMessage.status = EMMessage.Status.FAIL;
      localObject = new ContentValues();
      ((ContentValues)localObject).put("status", paramEMMessage.status.ordinal());
      e.a().a(paramEMMessage.msgId, (ContentValues)localObject);
      if (paramEMCallBack != null)
        EMMessageUtils.asyncCallback(paramEMCallBack, i, "send message error");
    }
    else
    {
      if (paramEMMessage.getChatType() == EMMessage.ChatType.GroupChat)
      {
        sendGroupMessage(paramEMMessage, paramEMCallBack);
        return;
      }
      try
      {
        if (paramEMMessage.msgId == null)
          paramEMMessage.msgId = EMMessageUtils.getUniqueMessageId();
        if (paramEMMessage.getType() != EMMessage.Type.CMD)
          EMConversationManager.getInstance().saveMessage(paramEMMessage);
        paramEMMessage.status = EMMessage.Status.INPROGRESS;
        paramEMMessage.from = EMSessionManager.getInstance().currentUser;
        localObject = new EMSendMessageRunnable(paramChat, paramEMMessage, paramEMCallBack);
        this.sendThreadPool.execute((Runnable)localObject);
      }
      catch (Exception localException)
      {
        paramEMMessage.status = EMMessage.Status.FAIL;
        ContentValues localContentValues = new ContentValues();
        localContentValues.put("status", paramEMMessage.status.ordinal());
        e.a().a(paramEMMessage.msgId, localContentValues);
        localException.printStackTrace();
        if (paramEMCallBack != null)
          EMMessageUtils.asyncCallback(paramEMCallBack, -2, localException.getLocalizedMessage());
      }
    }
  }

  public void asyncFetchMessage(final EMMessage paramEMMessage)
  {
    final FileMessageBody localFileMessageBody = (FileMessageBody)paramEMMessage.body;
    String str1 = localFileMessageBody.localUrl;
    String str2 = localFileMessageBody.remoteUrl;
    String str3 = localFileMessageBody.fileName;
    if (TextUtils.isEmpty(str2))
    {
      if (localFileMessageBody.downloadCallback != null)
        localFileMessageBody.downloadCallback.onError(-1, "remoteUrl is null or empty");
      return;
    }
    if (paramEMMessage.type == EMMessage.Type.IMAGE)
    {
      if (!TextUtils.isEmpty(((ImageMessageBody)localFileMessageBody).thumbnailUrl))
        str2 = ((ImageMessageBody)localFileMessageBody).thumbnailUrl;
    }
    else if ((paramEMMessage.type != EMMessage.Type.VOICE) && (paramEMMessage.type == EMMessage.Type.VIDEO))
      str2 = ((VideoMessageBody)localFileMessageBody).thumbnailUrl;
    paramEMMessage.status = EMMessage.Status.INPROGRESS;
    final String str4;
    if (paramEMMessage.type == EMMessage.Type.IMAGE)
    {
      str3 = "th" + str2.substring(str2.lastIndexOf("/") + 1, str2.length());
      str4 = PathUtil.getInstance().getImagePath() + "/" + str3;
    }
    else if (paramEMMessage.type == EMMessage.Type.VIDEO)
    {
      str3 = str2.substring(str2.lastIndexOf("/") + 1, str2.length());
      str4 = PathUtil.getInstance().getImagePath() + "/" + str3;
      ((VideoMessageBody)localFileMessageBody).localThumb = str4;
      ((VideoMessageBody)localFileMessageBody).localUrl = (PathUtil.getInstance().getVideoPath() + "/" + str3 + ".mp4");
    }
    else if (paramEMMessage.type == EMMessage.Type.VOICE)
    {
      str3 = str2.substring(str2.lastIndexOf("/") + 1, str2.length());
      str4 = PathUtil.getInstance().getVoicePath() + "/" + str3;
      localFileMessageBody.localUrl = str4;
    }
    else if (paramEMMessage.type == EMMessage.Type.FILE)
    {
      str4 = PathUtil.getInstance().getFilePath() + "/" + str3;
      localFileMessageBody.localUrl = str4;
    }
    else
    {
      str4 = str1;
    }
    if ((TextUtils.isEmpty(str3)) || (str3.equals("th")))
    {
      paramEMMessage.status = EMMessage.Status.FAIL;
      updateMsgState(paramEMMessage);
      if (localFileMessageBody.downloadCallback != null)
        localFileMessageBody.downloadCallback.onError(-1, "fileName is null or empty");
      return;
    }
    final String str5 = str4;
    HttpFileManager localHttpFileManager = new HttpFileManager(EMChatConfig.getInstance().applicationContext, f.a().c().a);
    EMLog.d(TAG, "localUrl:" + localFileMessageBody.localUrl + " remoteurl:" + str2);
    HashMap localHashMap = new HashMap();
    String str6;
    if (paramEMMessage.type == EMMessage.Type.IMAGE)
    {
      str6 = ((ImageMessageBody)localFileMessageBody).thumbnailSecret;
      if (TextUtils.isEmpty(str6))
        str6 = localFileMessageBody.secret;
      if (!TextUtils.isEmpty(str6))
        localHashMap.put("share-secret", str6);
    }
    else if (paramEMMessage.type == EMMessage.Type.VIDEO)
    {
      str6 = ((VideoMessageBody)localFileMessageBody).thumbnailSecret;
      if (!TextUtils.isEmpty(str6))
        localHashMap.put("share-secret", str6);
    }
    else if ((paramEMMessage.type == EMMessage.Type.VOICE) && (localFileMessageBody.secret != null))
    {
      localHashMap.put("share-secret", localFileMessageBody.secret);
    }
    if ((paramEMMessage.type == EMMessage.Type.IMAGE) || (paramEMMessage.type == EMMessage.Type.VIDEO))
      localHashMap.put("thumbnail", "true");
    localHttpFileManager.downloadFile(str2, str5, localHashMap, new CloudOperationCallback()
    {
      public void onSuccess(String paramAnonymousString)
      {
        File localFile = new File(str5);
        EMLog.d(EMMessageHandler.TAG, "file downloaded:" + str5 + " size:" + localFile.length());
        if (EMChatManager.getInstance().getChatOptions().getUseEncryption())
          EMEncryptUtils.decryptFile(localFile.getAbsolutePath(), paramEMMessage.getFrom());
        localFileMessageBody.downloaded = true;
        paramEMMessage.status = EMMessage.Status.SUCCESS;
        EMMessageHandler.this.updateMsgState(paramEMMessage);
        paramEMMessage.progress = 100;
        if (localFileMessageBody.downloadCallback != null)
        {
          localFileMessageBody.downloadCallback.onProgress(100, null);
          localFileMessageBody.downloadCallback.onSuccess();
        }
        if ((paramEMMessage.type == EMMessage.Type.VOICE) || (paramEMMessage.type == EMMessage.Type.VIDEO))
          EMMessageHandler.this.updateMsgBody(paramEMMessage);
      }

      public void onError(String paramAnonymousString)
      {
        paramEMMessage.status = EMMessage.Status.FAIL;
        EMLog.e(EMMessageHandler.TAG, "download file localThumbnailFilePath:" + str5 + ",error:" + paramAnonymousString);
        if ((str4 != null) && (new File(str4).exists()))
        {
          File localFile = new File(str4);
          try
          {
            if (localFile.isFile())
              localFile.delete();
          }
          catch (Exception localException)
          {
            EMLog.d(EMMessageHandler.TAG, "temp file del fail." + str4);
          }
        }
        EMMessageHandler.this.updateMsgState(paramEMMessage);
        if (localFileMessageBody.downloadCallback != null)
          localFileMessageBody.downloadCallback.onError(-1, paramAnonymousString);
      }

      public void onProgress(int paramAnonymousInt)
      {
        paramEMMessage.progress = paramAnonymousInt;
        if (localFileMessageBody.downloadCallback != null)
          localFileMessageBody.downloadCallback.onProgress(paramAnonymousInt, null);
      }
    });
  }

  public void ackMessageRead(String paramString1, String paramString2, String paramString3)
    throws EaseMobException
  {
    Message localMessage = new Message();
    String str1 = EMContactManager.getEidFromUserName(paramString2);
    try
    {
      a locala = new a("acked");
      locala.setValue("id", paramString3);
      localMessage.addExtension(locala);
      localMessage.setBody(paramString3);
      EMLog.d(TAG, "send ack msg to:" + paramString2 + " for msg:" + paramString3);
      localMessage.setType(Message.Type.normal);
      localMessage.setTo(str1);
      String str2 = EMContactManager.getEidFromUserName(paramString1);
      localMessage.setFrom(str2);
      EMSessionManager.getInstance().getConnection().sendPacket(localMessage);
      e.a().f(paramString3, true);
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
      throw new EaseMobException(localException.getMessage());
    }
  }

  private void updateMsgState(EMMessage paramEMMessage)
  {
    ContentValues localContentValues = new ContentValues();
    localContentValues.put("status", Integer.valueOf(paramEMMessage.status.ordinal()));
    e.a().a(paramEMMessage.getMsgId(), localContentValues);
  }

  private void updateMsgBody(EMMessage paramEMMessage)
  {
    ContentValues localContentValues = new ContentValues();
    localContentValues.put("msgbody", MessageEncoder.getJSONMsg(paramEMMessage, true));
    e.a().a(paramEMMessage.getMsgId(), localContentValues);
  }

  void onConnected()
  {
    EMSendMessageRunnable.notifySendMsgLocks();
    EMSendMessageRunnable.flushPendingQueue();
  }

  public void onInit()
  {
    EMSendMessageRunnable.onInit();
  }

  public void onDestroy()
  {
    EMSendMessageRunnable.onDestroy();
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     com.easemob.chat.EMMessageHandler
 * JD-Core Version:    0.6.1
 */